package com.dominator.controller.backend;

import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.JsonUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.service.CarService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiUtils;
import com.dominator.utils.exception.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by czh on 2017/8/25.
 */
@RestController
@RequestMapping("/backend/v1/car")
public class CarController {

    private final static Logger log = LoggerFactory.getLogger(CarController.class);

    @Autowired
    private CarService carService;

    /**
     * 车辆列表
     *
     * @param paramStr user_id String 必传 用户id
     * @return
     */
    @GetMapping("/listCars")
    public ApiMessage listCars(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            ApiUtils.checkParam(dto, "user_id");
            msg = carService.listCars(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR.getCode(), ReqEnums.SYS_ERROR.getMsg());
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 添加车辆
     *
     * @param paramStr user_id String 必传 用户id
     *                 car_num String 必传 车牌号
     *                 car_vin String 必传 车vin
     *                 car_info String 非必传 车辆备注信息
     * @return
     */
    @PostMapping("/addCar")
    public ApiMessage addCar(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            ApiUtils.checkParam(dto, "user_id,car_num,car_vin");
            msg = carService.addCar(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR.getCode(), ReqEnums.SYS_ERROR.getMsg());
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 编辑车辆
     * @param paramStr car_id String 必传 车辆id
     *                 car_num String 必传 车牌号
     *                 car_vin String 必传 车vin
     *                 car_info String 非必传 车辆备注信息
     * @return
     */
    @PostMapping("/editCar")
    public ApiMessage editCar(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            ApiUtils.checkParam(dto, "car_id,car_num,car_vin");
            msg = carService.editCar(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR.getCode(), ReqEnums.SYS_ERROR.getMsg());
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 删除车辆
     *
     * @param paramStr car_id String 必传 车辆id
     * @return
     */
    @PostMapping("/delCar")
    public ApiMessage delCar(@RequestParam String paramStr) {
        ApiMessage msg;
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            ApiUtils.checkParam(dto, "car_id");
            msg = carService.delCar(dto);
        } catch (ApiException e) {
            msg = new ApiMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR.getCode(), ReqEnums.SYS_ERROR.getMsg());
            e.printStackTrace();
        }
        return msg;
    }

}
